home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc_ti.zip / TI862.ASC < prev   
Text File  |  1992-02-25  |  8KB  |  331 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                            NUMBER  :  862
  9.   VERSION  :  3.0
  10.        OS  :  DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/5
  12.  
  13.     TITLE  :  Getting Input In Graphics Mode
  14.  
  15.  
  16.  
  17.  
  18.   This program demonstrates how to get input from the user in
  19.   graphics mode, echoed in the current colors and font size and
  20.   font style.
  21.  
  22.   Functions:
  23.        newLine() advances the (graphic) text position to the next
  24.        line.
  25.  
  26.        getGrString(): echoes graphically the user input and stores
  27.        it in a buffer.
  28.  
  29.        doCursor(): a helper function for getGrString, to handle the
  30.        cursor.
  31.  
  32.        main(): the use of getGrString is demonstrated for string
  33.        and numeric input.
  34.  
  35.   NOTE:  Although it is believed that this software is fully
  36.   functional as described in the comments, no guarantees are made,
  37.   express or implied.
  38.  
  39.   /* ....................................................... */
  40.  
  41.   #define ON 1
  42.   #define OFF 0
  43.   #include <graphics.h>
  44.   #include <stdio.h>
  45.   #include <conio.h>
  46.  
  47.   void doCursor(int);
  48.   void newLine();
  49.   void getGrString(char *);
  50.  
  51.   int main(void)
  52.   {
  53.      char nameString[80],ageString[80];
  54.      int age;
  55.  
  56.      /* request auto detection */
  57.      int gdriver = DETECT, gmode, errorcode;
  58.  
  59.      /* initialize graphics and local variables */
  60.      initgraph(&gdriver, &gmode, "");
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                            NUMBER  :  862
  75.   VERSION  :  3.0
  76.        OS  :  DOS
  77.      DATE  :  February 25, 1992                        PAGE  :  2/5
  78.  
  79.     TITLE  :  Getting Input In Graphics Mode
  80.  
  81.  
  82.  
  83.  
  84.      /* read result of initialization */
  85.      errorcode = graphresult();
  86.      if (errorcode != grOk)  /* an error occurred */
  87.      {
  88.         printf("Graphics error: %s\n", grapherrormsg(errorcode));
  89.         printf("Press any key to halt:");
  90.         getch();
  91.         return(1); /* terminate with an error code */
  92.      }
  93.  
  94.      /* use some colors to show that getGrString() handles
  95.         foreground and background colors successfully. */
  96.      setbkcolor(BLUE);
  97.      setcolor(YELLOW);
  98.  
  99.      /* left-to-right gothic font, user-sizeable */
  100.      /* change this as you like, except getGrString assumes
  101.         left-to-right text direction! */
  102.      settextstyle(GOTHIC_FONT,HORIZ_DIR,0);
  103.  
  104.      /* get a reasonable screen position */
  105.      moveto(0,0);
  106.      outtext("Your name please? ");
  107.      getGrString(nameString);
  108.      newLine();
  109.  
  110.      /* just to demonstrate that you can get numeric input from a
  111.         string! */
  112.      outtext("Your age please? ");
  113.      getGrString(ageString);
  114.  
  115.      /* note: if atoi() returns 0, the string may not have been a
  116.         valid number!  A real program should check for this. */
  117.      age=atoi(ageString);
  118.      newLine();
  119.      outtext("Name: ");
  120.      outtext(nameString);
  121.  
  122.      /* increment age to work with it as a number */
  123.      ++age;
  124.  
  125.      /* make it a string again */
  126.      sprintf(ageString,"%d",age);
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                            NUMBER  :  862
  141.   VERSION  :  3.0
  142.        OS  :  DOS
  143.      DATE  :  February 25, 1992                        PAGE  :  3/5
  144.  
  145.     TITLE  :  Getting Input In Graphics Mode
  146.  
  147.  
  148.  
  149.  
  150.      newLine();
  151.      outtext("Next year, you will be ");
  152.      outtext(ageString);
  153.      newLine();
  154.      outtext("Press key to exit! ");
  155.      getch();
  156.      closegraph();
  157.      return 0;
  158.   }
  159.  
  160.   /* newLine: primitive yet serviceable routine for a new text line
  161.               in graphics mode */
  162.   void newLine()
  163.   {
  164.       moveto(0,gety()+textheight("A"));
  165.   }
  166.  
  167.   /* getGrString: takes a parameter of an input buffer, echoes
  168.        characters typed, and fills input buffer.
  169.  
  170.        Function returns upon <ENTER> or <LINEFEED>.
  171.  
  172.        Function responds appropriately to backspace.
  173.  
  174.        No provision is made to guard against overflow of
  175.        the buffer or going over the right screen border. */
  176.  
  177.   void getGrString(char *inputString)
  178.   {
  179.      /* stringIndex is the current place in the string, so that we
  180.         may build it as we go along getting input characters */
  181.      int stringIndex=0;
  182.  
  183.      /* xVal will store the screen position for each char as we go
  184.         along, so that we can erase and move the cursor
  185.         successfully during backspacing */
  186.      int xVal[255];
  187.  
  188.      /* inputChar: the character typed;  outString: the string
  189.         version of that character */
  190.      char inputChar, outString[2];
  191.  
  192.      /* oldColor saves the previous color value, to restore after
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                            NUMBER  :  862
  207.   VERSION  :  3.0
  208.        OS  :  DOS
  209.      DATE  :  February 25, 1992                        PAGE  :  4/5
  210.  
  211.     TITLE  :  Getting Input In Graphics Mode
  212.  
  213.  
  214.  
  215.  
  216.         erasing */
  217.      int oldColor;
  218.  
  219.      /* outString is just one char + a null-terminator */
  220.      outString[1]=0;
  221.  
  222.      /* screen starting position for input char string */
  223.      xVal[0]=getx();
  224.      do
  225.      {
  226.         /* turn on the cursor */
  227.         doCursor(ON);
  228.         /* get a single character, in no-echo mode */
  229.         inputChar=getch();
  230.         /* turn off the cursor before we write a new character */
  231.         doCursor(OFF);
  232.         /* avoid dealing with all special keys */
  233.         if (inputChar==0) getch();
  234.         else
  235.         {
  236.             if (inputChar==8) { /* backspace */
  237.                 /* save old character color */
  238.                 oldColor=getcolor();
  239.                 /* back up in the string */
  240.                 --stringIndex;
  241.                 /* no backing up past beginning of string! */
  242.                 if (stringIndex<0) stringIndex=0;
  243.                 /* move to (old horz position, current vert
  244.                    position) */
  245.                 moveto(xVal[stringIndex],gety());
  246.                 /* erasing consists of rewriting the old character
  247.                    in the background color */
  248.                 setcolor(getbkcolor());
  249.                 outString[0]=inputString[stringIndex];
  250.                 outtext(outString);
  251.                 /* correct the current screen position since it
  252.                    will have advanced after writing outString */
  253.                 moveto(xVal[stringIndex],gety());
  254.                 /* restore the text color we had */
  255.                 setcolor(oldColor);
  256.             }
  257.             else { /* put a character into the string and draw it
  258.                       on screen */
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                            NUMBER  :  862
  273.   VERSION  :  3.0
  274.        OS  :  DOS
  275.      DATE  :  February 25, 1992                        PAGE  :  5/5
  276.  
  277.     TITLE  :  Getting Input In Graphics Mode
  278.  
  279.  
  280.  
  281.  
  282.                 /* stuff the input into the string */
  283.                 inputString[stringIndex]=inputChar;
  284.                 /* draw the character on screen, as a string
  285.                    (since that's what outttext() needs) */
  286.                 outString[0]=inputChar;
  287.                 outtext(outString);
  288.                 /* proceed to next char in the string */
  289.                 ++stringIndex;
  290.                 /* save horz position for possible backspacing */
  291.                 xVal[stringIndex]=getx();
  292.             }
  293.          }
  294.      /* end getting characters on ENTER or LF */
  295.      } while(inputChar!=13 && inputChar!=10);
  296.      /* null-terminate input string before returning */
  297.      inputString[stringIndex]=0;
  298.   }
  299.  
  300.   /* doCursor: draw or undraw the cursor, depending on whether the
  301.   parameter
  302.      is non-zero (ON) or zero (OFF)
  303.   */
  304.  
  305.   void doCursor(int on) {
  306.       int curX,oldColor;
  307.       /* we'll use an underbar as a cursor */
  308.       char uBarStr[2] = { '_',0 };
  309.       /* if cursor goes OFF, erase by drawing w/bkground color */
  310.       if (!on) {
  311.           oldColor=getcolor();
  312.           setcolor(getbkcolor());
  313.       }
  314.       /* save horizontal position before drawing cursor */
  315.       curX=getx();
  316.       outtext(uBarStr);
  317.       moveto(curX,gety());
  318.       /* if we changed the color to erase cursor, change it back */
  319.       if (